#include "TankScore.h"
#include "global.h"
#include "SimpleAudioEngine.h"
#include "Tank1.h"
void TankScore::update(float dt)
{
    time+=dt;
    int score = 0;
    if (num < 4)
        score = map_levelScore[num];
    if (time>0.5)
    {
        time=0;
        if (num == 0)
        {
            if (global_sound) {
                //todo SoundEngine.playEffect(Resource.s_buttonEffect);
                CocosDenshion::SimpleAudioEngine::sharedEngine()->playEffect("SoundScore.mp3");
            }
            //num1 = new Num(the.num1);
            num1->draw(tankNum);
            //score1 = new MyNum2(the->score1);
            score1->draw(tankNum * 1);
            tankNum++;
            if (tankNum > score)
            {
                tankNum = 0;
                num++;
                //time = 0;
            }
        }
        
        if (num == 1)
        {
            if (global_sound) {
                //todo SoundEngine.playEffect(Resource.s_buttonEffect);
                CocosDenshion::SimpleAudioEngine::sharedEngine()->playEffect("SoundScore.mp3");
            }
            //num2 = new MyNum(the->num2);
            num2->draw(tankNum);
            //score2 = new MyNum2(the->score2)
            score2->draw(tankNum * 2);
            tankNum++;
            if (tankNum > score)
            {
                tankNum = 0;
                num++;
                //time = 0;
            }
        }
        
        if (num == 2)
        {
            if (global_sound) {
                //todo SoundEngine.playEffect(Resource.s_buttonEffect);
                CocosDenshion::SimpleAudioEngine::sharedEngine()->playEffect("SoundScore.mp3");
            }
            //num4 = new MyNum(the->num4); //
            num3->draw(tankNum);
            //score4 = new MyNum2(the->score4); //
            score3->draw(tankNum * 3);
            tankNum++;
            if (tankNum > score)
            {
                tankNum = 0;
                num++;
               // time = 0;
            }
        }
        if (num == 3)
        {
            if (global_sound) {
                //todo SoundEngine.playEffect(Resource.s_buttonEffect);
                CocosDenshion::SimpleAudioEngine::sharedEngine()->playEffect("SoundScore.mp3");
            }
            //num4 = new MyNum(the->num4); //
            num4->draw(tankNum);
            //score4 = new MyNum2(the->score4); //
            score4->draw(tankNum * 5);
            tankNum++;
            if (tankNum > score)
            {
                tankNum = 0;
                num++;
               // time = 0;
            }
        }

        if(num>3)
        {
            this->unscheduleUpdate();
            this->totalNum->draw(map_levelScore[0]+map_levelScore[1]+map_levelScore[2]+map_levelScore[3]);
            this->totalScore->draw(map_levelScore[0]*100+map_levelScore[1]*200+map_levelScore[2]*300+map_levelScore[3]*500);
            this->runAction(CCSequence::create(
                                               CCDelayTime::create(3.0f),
                                               CCCallFunc::create(this, callfunc_selector(TankScore::destroy)),NULL));
        }
    }
}

void TankScore::destroy() {
    this->getParent()->removeChild(this,true);//, <#bool cleanup#>)
	global_instance->nextLevel();
}
bool TankScore::ccTouchBegan(CCTouch* touch, CCEvent* event)
{
    if(cantouch)
    {
        this->destroy();
    }
	return true;
}
TankScore::TankScore():Dialog() {
	this->m_target=CCRenderTexture::create(420,700);
	m_target->setPosition(ccp(210,350));
	//m_target->begin();
    m_target->clear(0,0,0,1);
	//ccColor4F color2=ccc4FFromccc4B(ccc4(0,0,0,1));
	//m_target->end();
	//drawRect(m_target,ccp(0,0),ccp(420,700),color2);
    addChild(m_target);
	sprintf(tmpbuffer,"STAGE %d",map_level);
	float x,y;
	x=208;y=500;//556 416-60+5+195;
	CCLabelBMFont *s1 = CCLabelBMFont::create(tmpbuffer,"font1.fnt");// CCLabelTTF::labelWithString("0", "Arial", 14
	s1->setPosition(ccp(x ,y));s1->setScale(0.7);
	s1->setColor(ccc3(255, 255, 255));	addChild(s1);

	this->addrow(this->num1,this->score1,1);
	this->addrow(this->num2,this->score2,2);
	this->addrow(this->num3,this->score3,3);
	this->addrow(this->num4,this->score4,4);
	
	//num2 = new Num(174+5+2, 416-213+5+195);num2->autorelease();
 //   num3 = new Num(174+5+2, 416-259+5+195);num3->autorelease();
 //   num4 = new Num(174+5+2, 416-306+5+195);num4->autorelease();
 //   score2 = new Num2(51+5+2, 416-213+5+195);score2->autorelease();
 //   score3 = new Num2(51+5+2, 416-259+5+195);score3->autorelease();
 //   score4 = new Num2(51+5+2, 416-306+5+195);score4->autorelease();
 //   //stageNum = new Num(274+5+2, 416-60+5+195);stageNum->autorelease();
 //   totalNum = new Num(174+5+2, 416-338+5+195);totalNum->autorelease();
 //   totalScore = new Num2(135+5+2, 416-123+5+195);totalScore->autorelease();
	m_target->begin();
	::glLineWidth(2);
	//::glColor4f(color.r,color.g,color.b,color.a);
	//::ccDrawCircle(p1, 8,360,8,true);
	y=y-4*70+56;
	CCPoint p2=ccp(x-200,y);
	CCPoint p1=ccp(x+200,y);
	::ccDrawLine(p1,p2);
	//::ccDrawCircle(<#const cocos2d::CCPoint &center#>, <#float radius#>, <#float angle#>, <#int segments#>, <#bool drawLineToCenter#>)
	m_target->end();
	y=y-30;
	totalScore = new NumX(65, y,6);totalScore->autorelease();addChild(totalScore);
	totalNum = new NumX(240, y,2);totalNum->autorelease();addChild(totalNum);
	
    //addChild(stageNum);
    //this->setPosition(ccp(-34, -17));
    time = 0;
    num = 0;
    tankNum = 0;
    this->scheduleUpdate();
}
void TankScore::addrow(Num * &num1,Num2* &score1,int i)
{
	float x,y;
	x=80+20;y=-50+416-60+5+195-i*50;
	score1 = new Num2(x, y);score1->autorelease();addChild(score1);

	CCLabelBMFont *s1 = CCLabelBMFont::create("PTS","font1.fnt");// CCLabelTTF::labelWithString("0", "Arial", 14
	x=x+80;
	s1->setPosition(ccp(x ,y));s1->setScale(0.7);
	s1->setColor(ccc3(255, 255, 255));	addChild(s1);

	x=x+30+30;
	num1 = new Num(x, y);num1->autorelease();addChild(num1);

	x=x+30;
	CCSprite * jt1=CCSprite::createWithSpriteFrameName("jiantou1.png");
	jt1->setPosition(ccp(x,y));addChild(jt1);

	x=x+30;
	if(i==1){
		Tank1 * tk1=new Tank1(x,y);tk1->autorelease();addChild(tk1);
	}
	else if(i==2)
	{
		Tank2 * tk1=new Tank2(x,y);tk1->autorelease();addChild(tk1);
	}
	else if(i==3)
	{
		Tank3 * tk1=new Tank3(x,y);tk1->autorelease();addChild(tk1);
	}
    else
    {
        BonusA * tk1=new BonusA(x,y);tk1->autorelease();addChild(tk1);
    }
}
